package com.panda.business.service;

import com.panda.business.domain.req.HistoryMessageVo;
import com.panda.business.domain.req.WeChatMessageVo;
import com.panda.business.mapper.WeChatMessageMapper;
import com.panda.business.domain.WeChatMessageInfo;
import com.panda.framework.domain.base.DBPage;
import com.panda.framework.domain.base.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional // 支持事务
public class WeChatMessageService {

	@Autowired
	private WeChatMessageMapper weChatMessageMapper;

	public PageBean<WeChatMessageInfo> queryWeChatMessageInfo(WeChatMessageVo info) {
		DBPage page = new DBPage(info.getPageSize(), (info.getPageNo() - 1) * info.getPageSize());

		List<WeChatMessageInfo> resultList = weChatMessageMapper.queryWeChatMessageInfo(page, info.getCid(),
				info.getWechatId(), info.getFriendId());
		Integer totalResult = weChatMessageMapper.queryWeChatMessageInfoCount(page, info.getCid(), info.getWechatId(),
				info.getFriendId());
		int pageTotal = totalResult % info.getPageSize() == 0 ? totalResult / info.getPageSize()
				: totalResult / info.getPageSize() + 1;

		PageBean<WeChatMessageInfo> pageBean = new PageBean<WeChatMessageInfo>(info.getPageSize(), info.getPageNo(),
				pageTotal, resultList);

		return pageBean;
	}

	public List<WeChatMessageInfo> queryHistormMessage(DBPage page, HistoryMessageVo info) {
		return weChatMessageMapper.queryHistormMessage(page,info);
	}
	
	public Integer queryHistormMessageCount(HistoryMessageVo info) {
		return weChatMessageMapper.queryHistormMessageCount(info);
	}
	
	public WeChatMessageInfo queryWeChatMessageInfoByMsgServerId(String wechatid, String friendid, String msgSvrId) {
		return weChatMessageMapper.queryWeChatMessageInfoByMsgServerId(wechatid, friendid, msgSvrId);
	}
	
	public void update(WeChatMessageInfo info) {
		try {
			weChatMessageMapper.update(info);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	public void insert(WeChatMessageInfo info) {
		try {
			weChatMessageMapper.insert(info);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void delete(Integer id) {
		WeChatMessageInfo info = new WeChatMessageInfo();
		info.setId(id);
		weChatMessageMapper.delete(info);
	}

}
